﻿using System;
using Cysharp.Threading.Tasks;
using Framework.Manager;
using Framework.UI;
using Shared.UI;
using UnityEngine;

namespace Framework.Task
{
    public class Task_LoadUI<T> : Task where T : UIController, new()
    {
        private Enum uiId;
        private Func<UniTask> handler;
        private UIArg args;

        public Task_LoadUI(int factor, Enum id, Func<UniTask> handler, UIArg args) : base(factor, "load ui")
        {
            uiId = id;
            this.handler = handler;
            this.args = args;
        }

        protected override async UniTask OnStart()
        {
            await UIManager.Instance.Active<T>(uiId, args);

            if (handler != null)
                await handler();

            await OnFinished();
        }

        protected override async UniTask OnPanic()
        {
            await UIManager.Instance.Deactive(uiId);
        }
    }
}